package package1;

import java.util.ArrayList;

import com.sciforma.psnext.api.DataFormatException;
import com.sciforma.psnext.api.Organization;
import com.sciforma.psnext.api.PSException;
import com.sciforma.psnext.api.Session;
import com.sciforma.psnext.api.SystemData;

public class Test5
{

   static Session mySession = null;

   public static void main(String[] args)
   {

      try
      {
         mySession = new Session("http://192.168.101.232:8080/psnext");

         System.out.println("En cours de connexion ...");
         mySession.login("psnextadmin", "azerty".toCharArray());
         System.out.println("Connecté");

         Organization mm = (Organization) mySession.getSystemData(SystemData.ORGANIZATIONS);

         System.out.println(findOrga("SCHNEIDER.APOD!", "", mm));

         mySession.logout();
         System.out.println("Déconnecté ");  

      }
      catch (PSException e)
      {
         // TODO Auto-generated catch block
         //
         e.printStackTrace();
      }
   }

   public static boolean findOrga(String orgaToFind, String tmp, Organization prent) throws DataFormatException, PSException
   {
      boolean result = false;

      String orgaName = prent.getStringField("Name");
      if (!tmp.equals("")) orgaName = tmp + "." + orgaName;

      System.out.println(orgaName);

      if (orgaToFind.equals(orgaName)) return true;

      @SuppressWarnings("unchecked")
      ArrayList<Organization> list = prent.getChildren();

      for (Organization org : list)
      {
         result = findOrga(orgaToFind, orgaName, org);
         if (result) break;
      }

      return result;

   }
}
